package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.entity.Bed;
import com.neuedu.entity.Beddetails;
import com.neuedu.entity.Customer;
import com.neuedu.mapper.CustomerMapper;
import com.neuedu.service.BedService;
import com.neuedu.service.BeddetailsService;
import com.neuedu.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author wangyu
 * @since 2025-10-15
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Resource
    BedService bedService;
    @Resource
    BeddetailsService beddetailsService;
    @Resource
    PlatformTransactionManager manager;
    @Override
    public boolean add(Customer customer) {
        // 通过身份证号码计算性别和生日
        String idcard = customer.getIdcard();
        int sex = Integer.valueOf(idcard.substring(16, 17)) % 2;
        String birthday = String.format("%s-%s-%s", idcard.substring(6, 10), idcard.substring(10, 12), idcard.substring(12, 14));
        customer.setSex(sex);
        customer.setBirthday(LocalDate.parse(birthday, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        // 创建床位详情 并赋值
        Beddetails beddetails = new Beddetails();
        beddetails.setStartDate(customer.getCheckinDate());
        beddetails.setEndDate(customer.getExpirationDate());
        beddetails.setBedId(customer.getBedId());
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        TransactionStatus status = manager.getTransaction(definition);
        try {
            bedService.updateStatus(customer.getBedId(), 2);
            this.save(customer);
            beddetails.setCustomerId(customer.getId());
            Bed bed = bedService.getById(customer.getBedId());
            beddetails.setBedDetails(String.format("%d#%s", bed.getRoomNo(), bed.getBedNo()));
            beddetailsService.save(beddetails);
            manager.commit(status);
            return true;
        } catch (Exception ex) {
            manager.rollback(status);
            throw ex;
        }

    }

    @Override
    public IPage<Customer> list(Integer pageNo, Integer pageSize) {
        return this.baseMapper.getPage(new Page<>(pageNo, pageSize));
    }

    @Override
    public boolean delete(Integer id) {
        /**
         * 将入住删除标识改成1
         * */
        Customer customer = new Customer();
        customer.setId(id);
        customer.setIsDeleted(1);
        /**
         * 将床位的状态改为空闲
         * */
        Bed bed = new Bed();
        bed.setId(this.getById(id).getBedId());
        bed.setBedStatus(1);
        /**
         *  将床位详情改为1
         *  */
        Beddetails beddetails = new Beddetails();
        beddetails.setIsDeleted(1);
        QueryWrapper<Beddetails> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", id);
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        TransactionStatus status = manager.getTransaction(definition);
        try {
            this.updateById(customer);
            bedService.updateById(bed);
            beddetailsService.update(beddetails, wrapper);
            manager.commit(status);
            return true;
        } catch (Exception ex) {
            manager.rollback(status);
            throw ex;
        }
    }
}
